﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RoadTeam.Enums;
using System.Data;
using RoadTeam.SysInterface;
using RoadTeam.Model;
using RoadTeam.Pub;

namespace RoadTeam.BLL
{
    public class RmCustBll
    {
        private DataTable GetCustDataSys(string StrUnitIdentifier)
        {
            SqlWhere where = new SqlWhere();
            where.StrSqlWhere = @"  UnitIdentifier IN (" + StrUnitIdentifier + ") ";
            return RmCust.Select(where, GlobalConfig.ConnectionString);
        }

        private bool UpdateCustData(DataTable dt)
        {
            return TbRmCust.Update(dt, GlobalConfig.RtConnectionString);
        }

        public DataTable SyncCustData(DataTable Unit_dt)
        {
            if (Unit_dt == null || Unit_dt.Rows.Count == 0) return new DataTable();

            List<String> LiUnitIdentifier = new List<string>();
            String StrUnitIdentifier = String.Empty;
            foreach (DataRow dr in Unit_dt.Rows)
            {
                if (dr["UnitIdentifier"] == null || LiUnitIdentifier.Contains(dr["UnitIdentifier"].ToString())) continue;
                LiUnitIdentifier.Add(dr["UnitIdentifier"].ToString());
                StrUnitIdentifier += "'" + dr["UnitIdentifier"].ToString() + "',";
            }

            if (String.IsNullOrEmpty(StrUnitIdentifier)) return new DataTable();

            StrUnitIdentifier = StrUnitIdentifier.TrimEnd(',');

            DataTable dt_Cust = FactoryClass.CreateRmCust().GetCustData(StrUnitIdentifier);
            DataTable dt_Cust_Sys = GetCustDataSys(StrUnitIdentifier);

            #region 同步表中已经存在的客户信息
            String Rtidentity = @"CustIdentifier";
            String idtntity = @"CustIdentifier";
            String EnmunType = @"CustColumnDictionary";
            RoadTeam.Pub.SyncTable.SyncDataTable(dt_Cust, dt_Cust_Sys, Rtidentity, idtntity, EnmunType);
            RmCust.Update(dt_Cust_Sys, GlobalConfig.ConnectionString);
            #endregion

            //如果不存在,添加新列并且插入表
            DataRow dr_Sys_Cust;
            String CustIdentifier = "CustIdentifier";
            foreach (DataRow dr_Cust in dt_Cust.Rows)
            {
                if (dr_Cust["CustIdentifier"] != null && !String.IsNullOrEmpty(dr_Cust["CustIdentifier"].ToString())) continue;

                //添加判断,如果同种类型的房间已经存在,则跳过此处赋值

                DataRow[] drs_Sys = dt_Cust_Sys.Select(" CustIdentifier IS NOT NULL AND RelaType='" + dr_Cust["RelaType"].ToString() + "' AND  UnitIdentifier='" + dr_Cust["UnitIdentifier"].ToString() + "'");
                if (drs_Sys.Count() != 0) continue;

                dr_Sys_Cust = dt_Cust_Sys.NewRow();
                foreach (DataColumn Rt_Cust_Dc in dt_Cust.Columns)
                {

                    foreach (DataColumn Dc in dt_Cust_Sys.Columns)
                    {
                        if (Rt_Cust_Dc.ColumnName.ToLower() == Dc.ColumnName.ToLower())
                        {
                            if (Rt_Cust_Dc.ColumnName.ToLower() != CustIdentifier.ToLower() && Dc.ColumnName.ToLower() != CustIdentifier.ToLower())
                            {
                                dr_Sys_Cust[Dc.ColumnName.ToLower()] = dr_Cust[Rt_Cust_Dc.ColumnName.ToLower()];
                            }
                            else
                            {
                                String Identifier = CreateIdentifier.GetIdentifier();
                                dr_Sys_Cust[Dc.ColumnName.ToLower()] = Identifier;
                                dr_Cust[Rt_Cust_Dc.ColumnName.ToLower()] = Identifier;
                            }
                        }
                    }
                }
                dt_Cust_Sys.Rows.Add(dr_Sys_Cust);
            }

            RmCust.InsertDatatable(dt_Cust_Sys, GlobalConfig.ConnectionString);
            UpdateCustData(dt_Cust);
            return dt_Cust_Sys;
        }
    }
}
